package cn.xpg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.xpg.entity.User;
import cn.xpg.service.UserService;
import cn.xpg.tools.Constants;
import cn.xpg.tools.SendSms;

@Controller
public class UserController {
	private Logger logger = Logger.getLogger(UserController.class);

	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping(value = "/login")
	public String login() {
		logger.debug("LoginController welcome AppInfoSystem develpor==================");
		return "login";
	}

	// 登录
	@RequestMapping(value = "/loginUser", method = RequestMethod.POST)
	public String loginUser(@RequestParam String user_name, @RequestParam String user_password,
			HttpServletRequest request, HttpSession session) {
		User user = null;
		try {
			user = userService.loginUser(user_name, user_password);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		/*
		 * SendSms sms = new SendSms(); try { sms.show("18124085071"); } catch
		 * (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
		 */
		if (null != user) {// 登录成功
			// 放入session
			session.setAttribute(Constants.USER_SESSION, user);
			System.out.println(user.getUser_id() + "---------------" + user.getUser_name());
			// 页面跳转（index.jsp）
			return "index";
		} else {
			// 页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名或密码不正确");
			return "login";
		}

	}

	@RequestMapping(value = "/manage/main")
	public String main(HttpSession session) {
		if (session.getAttribute(Constants.USER_SESSION) == null) {
			return "redirect:/login";
		}
		return "index";
	}

	// 登出
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		// 清除session
		session.removeAttribute(Constants.USER_SESSION);
		return "login";
	}

	// 显示用户信息
	@RequestMapping("getUserId.do")
	public String getUserId(HttpServletRequest request) {
		System.out.println("获取用户Id！！！！！！！！！！————>getUserId");
		int user_id = Integer.parseInt(request.getParameter("user_id"));
		String uname = request.getParameter("uname");
		User user = userService.getUserId(user_id);
		request.setAttribute("user", user);
		if (uname.equals("system")) {
			System.out.println("!!!!!!!!!!!!!!!system");
			return "/jsp/system/pwd_modify";
		} else {
			System.out.println("!!!!!!!!!!!!!!!user");
			return "/jsp/user/manage_yh";
		}
	}

	// 修改用户信息
	@RequestMapping("updateUser.do")
	public String updateUser(@Valid User user, HttpServletRequest request) {
		System.out.println("修改用户信息！！！！！！！！！！————>updateUser" + user.getUser_status());
		if (userService.updateUser(user) > 0) {
			System.out.println("修改成功！！！！！！");
			User user1 = userService.getUserId(user.getUser_id());
			request.setAttribute("user", user1);
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("user", user);
		}
		return "/jsp/user/manage_yh";
	}

	// 查询用户
	@RequestMapping("/getUserList.do")
	public String getUserList(HttpServletRequest request) {
		List<User> userList = userService.getUserList();
		request.setAttribute("userList", userList);
		for (User user : userList) {
			System.out.println(user.getUser_name());
		}
		return "/jsp/user/index";
	}

	// 修改密码
	@RequestMapping("/updatePwd.do")
	public String updatePwd(HttpServletRequest request) {
		System.out.println("修改用户密码！！！！！！！！！！————>updatePwd");
		int user_id = Integer.valueOf(request.getParameter("user_id").toString());
		System.out.println(user_id + "!!!!!!");
		String user_password = request.getParameter("user_password");
		User user = userService.getUserId(user_id);
		System.out.println(user.getUser_password() + "!!!!");
		request.setAttribute("oldpwd", user.getUser_password());
		if (userService.updatePwd(user_id, user_password) > 0) {
			System.out.println("修改密码成功！！！！！！");
		} else {
			System.out.println("修改密码失败！！！！！！");
			request.setAttribute("user_id", user_id);
			request.setAttribute("user_password", user_password);
		}
		return "/jsp/system/pwd_modify";
	}

	// 注册
	@RequestMapping("/register.do")
	public String register(@Valid User user) {
		System.out.println("注册用户账号！！！！！！！！！！————>register");
		if (userService.register(user) > 0) {
			return "/login";
		} else {
			return "/register";
		}
	}

	// 发送短信
	@RequestMapping("/sendSms.do")
	public String sendSms(HttpServletRequest request) throws Exception {
		String smsMob = request.getParameter("smsMob");
		String smsContent = request.getParameter("smsContent");
		if (smsMob != null || smsContent != null) {
			SendSms.sendSms(smsMob, smsContent);
		}
		return "/jsp/user/send_msg";
	}
}
